package com.lagou.wwyan.rpc.config;

import com.lagou.wwyan.rpc.register.RpcRegisterFactory;
import com.lagou.wwyan.rpc.register.ZookeeperRpcRegisterFactory;
import com.lagou.wwyan.rpc.server.RpcServer;
import com.lagou.wwyan.rpc.server.factory.RpcBeanFactory;
import com.lagou.wwyan.rpc.server.factory.SpringRpcBeanFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RpcConfiguration {
    @Value("${rpc.port:9999}")
    private int port;
    @Value("${rpc.host:127.0.0.1}")
    private String host;
    @Value("${zookeeper.url:127.0.0.1:2181}")
    private String zookeeperUrl;
    @Bean
    public RpcBeanFactory rpcBeanFactory(){
        return new SpringRpcBeanFactory();
    }

    @Bean
    public RpcRegisterFactory rpcRegisterFactory(){
        ZookeeperRpcRegisterFactory factory = new ZookeeperRpcRegisterFactory();
        factory.setUrl(zookeeperUrl);
        return factory;
    }

    @Bean
    public RpcServer rpcServer(RpcBeanFactory beanFactory,RpcRegisterFactory rpcRegisterFactory) throws InterruptedException {
        RpcServer rpcServer = new RpcServer(beanFactory, rpcRegisterFactory);
        rpcServer.startServer(host,port);
        return rpcServer;
    }


}
